import useUserStore from '@/store/user';
import Cookies from 'js-cookie';

/**
 * @description:
 * 1.判断是否有权限
 * 2.主要用于鼠标右键菜单,操作的下拉框
 * 3.全局复用，谨慎修改
 * 4.超级管理员无需校验
 * @param {string} perm
 */
export const hasPerm = (perm: string): boolean => {
  const store = useUserStore();
  let userInfo = store.$state.userInfo;
  if (userInfo) {
    if (userInfo.admin) {
      return false;
    }
    const target = userInfo.roles.find(e => {
      if (!e.permissions || e.permissions.length === 0) return false;
      if (e.permissions.includes(perm)) {
        return e;
      }
      return false;
    });
    return !target;
  }
  return false;
};

export const setCookiesToken = (token: string) => {
  Cookies.set('Authorization', token);
};
export const clearCookiesToken = () => {
  Cookies.remove('Authorization');
};
